﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Windows.Media.Imaging;
using System.Drawing.Imaging;
using WGMClient.Controls;
using System.Net.NetworkInformation;
using WGMClient.Properties;
using System.Xml.Serialization;
using System.Media;
using System.Runtime.InteropServices;


namespace WGMClient.HelperClasses
{
    public static class HelperFunctions
    {
        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();

        public static System.Drawing.Image ByteArrayToImage(byte[] byteArrayIn)
        {
            if (byteArrayIn != null)
            {
                MemoryStream ms = new MemoryStream(byteArrayIn);
                System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
                return returnImage;
            }
            System.Drawing.Image img = System.Drawing.Image.FromFile(System.Windows.Forms.Application.StartupPath + "\\Images\\blank2.png");
            return img;
        }
        public static string GetSoundURL(string filename)
        {
            string uri = System.Windows.Forms.Application.StartupPath +"\\Sounds\\"+filename;
            return uri;
        }
        public static byte[] ImageToByteArray(System.Drawing.Image imageIn, ImageFormat format)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, format);
            return ms.ToArray();
        }
        public static BitmapImage GetBitmapFromImage(System.Drawing.Image imageIn)
        {
            
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            MemoryStream ms = new MemoryStream();
            // Save to a memory stream...
            imageIn.Save(ms, imageIn.RawFormat);
            // Rewind the stream...
            ms.Seek(0, SeekOrigin.Begin);
            // Tell the WPF image to use this stream...
            bi.StreamSource = ms;
            bi.EndInit();
            return bi;
        }
        public static void SlideIn(UIElement element, double xOffset, double yOffset, TimeSpan duration)
        {
            if (!HelperClasses.Registry.Settings.WindowAnimations)
                return;
            Transform tempTrans = element.RenderTransform;
            TranslateTransform trans = new TranslateTransform(xOffset, yOffset);

            TransformGroup group = new TransformGroup();
            if (tempTrans != null) group.Children.Add(tempTrans);
            group.Children.Add(trans);

            DoubleAnimation animTranslate = new DoubleAnimation(0, (Duration)duration);
            element.RenderTransform = trans;
            trans.BeginAnimation(TranslateTransform.XProperty, animTranslate);
            trans.BeginAnimation(TranslateTransform.YProperty, animTranslate);
        }

        public static void SlideOut(UIElement element, double xOffset, double yOffset, TimeSpan duration)
        {
            if (!HelperClasses.Registry.Settings.WindowAnimations)
                return;
            Transform tempTrans = element.RenderTransform;
            TranslateTransform trans = new TranslateTransform(0, 0);

            TransformGroup group = new TransformGroup();
            if (tempTrans != null) group.Children.Add(tempTrans);
            group.Children.Add(trans);

            
            DoubleAnimation animTranslateX = new DoubleAnimation(xOffset, (Duration)duration);
            DoubleAnimation animTranslateY = new DoubleAnimation(yOffset, (Duration)duration);


            element.RenderTransform = trans;
            trans.BeginAnimation(TranslateTransform.XProperty, animTranslateX);
            trans.BeginAnimation(TranslateTransform.YProperty, animTranslateY);
        }
        public static void SlideOutHorizontalAndRemove(UIElement element, UIElementCollection container, double xOffset, TimeSpan duration)
        {

            if (!HelperClasses.Registry.Settings.WindowAnimations)
            {
                container.Remove(element);
                return;
            }
            Transform tempTrans = element.RenderTransform;
            TranslateTransform trans = new TranslateTransform(0, 0);

            TransformGroup group = new TransformGroup();
            if (tempTrans != null) group.Children.Add(tempTrans);
            group.Children.Add(trans);
            DoubleAnimation animTranslateX = new DoubleAnimation(xOffset, (Duration)duration);

            animTranslateX.Completed += delegate
            {
                container.Remove(element);
            };

            element.RenderTransform = trans;
            trans.BeginAnimation(TranslateTransform.XProperty, animTranslateX);
            

        }
        public static string GetMacAddress()
        {
            foreach (NetworkInterface nif in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (nif.OperationalStatus == OperationalStatus.Up)
                {
                    return nif.GetPhysicalAddress().ToString();
                }
            }
            return "";
        }

        public static void FadeIn(UIElement element,  TimeSpan duration)
        {
            if (!HelperClasses.Registry.Settings.WindowAnimations)
                return;
            (element as FrameworkElement).Visibility = Visibility.Visible;
            DoubleAnimation opacityAnim = new DoubleAnimation(element.Opacity,1, (Duration)duration);
            
            element.BeginAnimation(UIElement.OpacityProperty, opacityAnim);
        }
        public static void FadeOut(UIElement element,  TimeSpan duration)
        {
            if (!HelperClasses.Registry.Settings.WindowAnimations)
                return;
            DoubleAnimation opacityAnim = new DoubleAnimation(1,0, (Duration)duration);
            opacityAnim.Completed += delegate { (element as FrameworkElement).Visibility = Visibility.Collapsed; };
            element.BeginAnimation(UIElement.OpacityProperty, opacityAnim);
        }
        public static void FadeOut(UIElement element,double fadeValue, TimeSpan duration)
        {
            if (!HelperClasses.Registry.Settings.WindowAnimations)
                return;
            DoubleAnimation opacityAnim = new DoubleAnimation(element.Opacity, fadeValue, (Duration)duration);
 
            element.BeginAnimation(UIElement.OpacityProperty, opacityAnim);
        }
        public static void FadeOutIn(UIElement element,  TimeSpan duration)
        {
            if (!HelperClasses.Registry.Settings.WindowAnimations)
                return;
            DoubleAnimation opacityOut = new DoubleAnimation(1, 0, (Duration)duration);
            DoubleAnimation opacityIn = new DoubleAnimation(0, 1, (Duration)duration);
            opacityOut.Completed += delegate { element.BeginAnimation(UIElement.OpacityProperty, opacityIn); };
            element.BeginAnimation(UIElement.OpacityProperty, opacityOut);
            
        }
        public static void SizeTo(UIElement element, double width, double height, TimeSpan duration)
        {
            if (!HelperClasses.Registry.Settings.WindowAnimations)
            {
                element.SetValue(FrameworkElement.WidthProperty, width);
                element.SetValue(FrameworkElement.HeightProperty, height);
                return;
            }
            DoubleAnimation animTranslateX = new DoubleAnimation(width, (Duration)duration);
            DoubleAnimation animTranslateY = new DoubleAnimation(height, (Duration)duration);

            element.BeginAnimation(FrameworkElement.WidthProperty, animTranslateX);
            element.BeginAnimation(FrameworkElement.HeightProperty, animTranslateY);
        }
        public static void LoadSettings()
        {
            try
            {
                if (File.Exists(Registry.SettingsFile))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(WGMSettings));
                    FileStream fs = new FileStream(Registry.SettingsFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    try
                    {
                        Registry.Settings = (WGMSettings)serializer.Deserialize(fs);
                    }
                    catch
                    {
                        Registry.Settings = new WGMSettings();
                        SaveSettings();
                    }
                }
                else
                {
                    Registry.Settings = new WGMSettings();
                    SaveSettings();
                }
            }
            catch { }
        }
        public static void SaveSettings()
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(WGMSettings));
                FileStream fs = new FileStream(Registry.SettingsFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                try
                {
                    serializer.Serialize(fs, Registry.Settings);
                }
                catch { }
            }
            catch (Exception ex)
            {
                
            }

        }


        internal static void LogException(Exception ex)
        {
            try
            {
                FileStream fs = new FileStream(Registry.WorkingDir + "\\" + "wgmerrlog.log", FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(DateTime.Now.ToString()+": "+ex.ToString());
                sw.WriteLine("======================================\n");
                sw.Close();
                fs.Close();
                FileInfo fi = new FileInfo(Registry.WorkingDir + "\\" + "wgmerrlog.log");
                if (fi.Length > 1024 * 1024 * 30)
                    fi.Delete();
            }
            catch { }
        }

        internal static void PlayMessageSound()
        {
            SoundPlayer player = new SoundPlayer(HelperClasses.HelperFunctions.GetSoundURL("Ding.wav"));
            player.Play();
        }
    }
}
